#!/bin/sh
# generic wrapper to run as spot (when currently running as root)
# (C) James Budiono 2012
# License: GPL version 3 or later
#130604 BK: slight mods. see also /usr/sbin/setup-spot (support/setup-spot in woof).

[ ! $1 ] && exit
CWD=$(pwd)
APP="$1"; shift
ARGS=""
[ $1 ] && while [ "$1" ]; do ARGS="$ARGS \"$1\""; shift; done
if [ $(id -u) -eq 0 ]; then
 [ $XAUTHORITY ] && cp $XAUTHORITY /root/spot/.Xauthority 2>/dev/null
 touch /root/spot/.Xauthority
 #following line is mostly there to catch any root:root files that may have got copied in...
 chown -R spot:spot /root/spot &
 export XAUTHORITY=/root/spot/.Xauthority  
 export XDG_CONFIG_HOME=/root/spot/.config
 export XDG_CACHE_HOME=/root/spot/.cache
 export XDG_DATA_HOME=/root/spot/.local/share
 #exec su spot -s /bin/sh -c "cd \"$CWD\"; \"$APP\" $ARGS"
 DBUS_SESSION_BUS_ADDRESS="" ; exec su spot -s /bin/sh -c "\"$APP\" $ARGS"
else #precaution
 exec "$APP" "$ARGS"
fi
